<?php
/**
 * Created by PhpStorm.
 * User: yuyiwen
 * Date: 2018/10/7
 * Time: 上午9:35
 */

namespace app\index\model;


class Member
{
    /**
     *  获取下级会员
     */
    public function getSonMember ($current_id)
    {
        # 查询一级会员
         $SonMember = db('member')->alias('a')->field('a.* , b.username as pusername')->join('member b' , 'a.pid = b.id')->where(['a.pid'=>$current_id])->select();

        # 查询二级会员
        foreach ($SonMember as $k1 => $v1) {
            $SonMember[$k1]['son'] = db('member')->alias('a')->field('a.* , b.username as pusername')->join('member b' , 'a.pid = b.id')->where(['a.pid'=>$v1['id']])->select();
            # 查询三级会员
            foreach ($SonMember[$k1]['son'] as $k2 => $v2) {
                $SonMember[$k1]['son'][$k2]['son'] = db('member')->alias('a')->field('a.* , b.username as pusername')->join('member b' , 'a.pid = b.id')->where(['a.pid'=>$v2['id']])->select();
            }
        }
        return $SonMember;
    }

    public function getLevelMember ($SonMember)
    {
        $levelMember = [];
        foreach ($SonMember as $k1 => $v1) {
            $levelMember['one'][] = $v1;
            foreach ($v1['son'] as $k2 => $v2) {
                $levelMember['two'][] = $v2;
                foreach ($v2['son'] as $k3 => $v3) {
                    $levelMember['three'][] = $v3;
                }
            }
        }
        return $levelMember;
    }


}